package com.sixth.service.Impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.sixth.dao.GoodsDao;
import com.sixth.entity.Goods;
import com.sixth.entity.Limit;
import com.sixth.entity.Staff;
import com.sixth.service.GoodsService;
import com.sixth.util.CommonResult;
import com.sixth.util.LayTab;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @program: project01
 * @description:
 * @author: 老魏
 * @create: 2021-12-20 09:11
 **/
@Service("/goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private HttpSession session;

    //获取商品信息
    public LayTab getAllGoods(Goods goods, Limit limit) {
        Staff staff = (Staff)session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Integer roleid = staff.getRoleid();
        List<Goods> allGoods = goodsDao.getAllGoods(goods, limit, branchid, roleid);
        Integer count = goodsDao.getGoodsCount(goods, branchid, roleid);
        LayTab layTab = new  LayTab(count, allGoods);
        return layTab;
    }

    //盘点信息的查找
    public LayTab getAllOutGoodsP(Goods goods, Limit limit, Integer bid) {
        Staff staff = (Staff)session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Integer roleid = staff.getRoleid();
        List<Goods> allGoods = this.goodsDao.getAllOutGoodsP(goods, limit, branchid, roleid,bid);
        Integer count =this.goodsDao.getOutGoodsCountP(goods, branchid, roleid,bid);
        LayTab layTab = new LayTab(count, allGoods);
        return layTab;

    }

    // 入库新增的全部查询
    public LayTab getGoods(Goods goods, Limit limit) {
        List<Goods> goodss = goodsDao.getGoods(goods, limit);
        Integer countGoods = goodsDao.getCountGoods(goods);
        return new LayTab(countGoods, goodss);
    }

    //获取商品信息
    public LayTab getAllOutGoods(Goods goods, Limit limit, Integer bid) {
        Staff staff = (Staff)session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Integer roleid = staff.getRoleid();
        List<Goods> allGoods = this.goodsDao.getAllOutGoods(goods, limit, branchid, roleid,bid);
        Integer count =this.goodsDao.getOutGoodsCount(goods, branchid, roleid,bid);
        LayTab layTab = new LayTab(count, allGoods);
        return layTab;
    }
    //删除
    public CommonResult delete (Integer goodsid ,Integer branchid){
        int i = goodsDao.deleteByPrimaryKey(goodsid,branchid);
        if(i>0){
            return new CommonResult(2000,"删除成功",null);
        }else {
            return  new CommonResult(5000,"删除失败" ,null);
        }
    }
   /* //修改
    public CommonResult update (Goods record){
        int  i= goodsDao.updateByPrimaryKeySelective(record);
        if(i>0){
            return  new CommonResult(2000,"修改成功",null);
        }else {
            return  new CommonResult(5000,"修改失败",null);
        }
    }
    //新增商品
    public CommonResult insert(Goods record)   {
        int i = goodsDao.insertSelective(record);
        if(i>0){
            return  new CommonResult(2000,"添加成功" ,null);
        }else {
            return  new CommonResult(5000,"添加失败" ,null);
        }
    }*/
    //修改和新增
   public CommonResult addOrUpdateGoods(Goods goods) throws IOException {
       System.out.println("新增商品" + goods);
       String main = goods.getMain().getOriginalFilename();
       System.out.println(main);
       int m = main.lastIndexOf(".");
       String s1 = main.substring(m);
       System.out.println(s1);
       main = UUID.randomUUID() + s1;
       String endpoint = "oss-cn-hangzhou.aliyuncs.com";
       // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
       String accessKeyId = "LTAI5t84hDoEnns9GxsWFxkv";
       String accessKeySecret = "UplvqkqHzxSkaK3GcsW1pvrsQWUf9D";
       String bucketName = "gp1124";
       // 创建OSSClient实例。
       OSS ossClient = (new OSSClientBuilder()).build(endpoint, accessKeyId, accessKeySecret);
       // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
       ossClient.putObject(bucketName, main, goods.getMain().getInputStream());
       //获取上传的文件名
       String url = "https://" + bucketName + "." + endpoint + "/" + main;
       ossClient.shutdown();
       goods.setUrl(url);
       Integer i;
       if (goods.getGoodsid() != null) {
           i = this.goodsDao.updateByPrimaryKeySelective(goods);
       } else {
           i = this.goodsDao.insertSelective(goods);
       }

       CommonResult result = null;
       if (i > 0) {
           result = CommonResult.success((Object)null, "操作成功");
       } else {
           result = CommonResult.fail("操作失败");
       }

       return result;
   }


    //测试名字是否重复
    public CommonResult getGoodsName(String goodsname) {
        List<Goods> goods = this.goodsDao.getGoodsName(goodsname);
        return goods.size() > 0 ? CommonResult.fail() : CommonResult.success();
    }


}
